package com.sandking.db.bean;

import com.sandking.tools.SK_Map;
import java.util.ArrayList;
import com.sandking.db.cache.YhCache;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import com.sandking.io.SK_OutputStream;
import com.sandking.db.cache.ZbCache;
import java.util.List;
import com.sandking.io.SK_InputStream;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.io.ByteArrayOutputStream;

/**
 * 战报
 */
public class Zb {

	public static final String TABLENAME = "战报";
	public static final String CLASSNAME = "Zb"; 
	/**
	 * 需要更新字段集
	 */
	private Map<String, Object> updateColumns;

	/** id */
	private int id;
	
	/** tempid */
	private int tempid;
	
	/** 攻击人id */
	private int gjrid;
	
	/** 目标人id */
	private int mbrid;
	
	/** 目标用户名称 */
	private String mbyhmc;
	
	/** 创建时间 */
	private java.util.Date cjsj;
	
	/** 是否可反击 */
	private boolean sfkfj;
	
	/** json */
	private String json;
	
	/** 星数 */
	private int xs;
	
	/** 是否结算 */
	private boolean sfjs;
	
	/** 金币 */
	private int jb;
	
	/** 水 */
	private int s;
	
	/** 开始战斗时间 */
	private long kszdsj;
	
	
	public Zb() {
		super();
	}
	
	public Zb(int id, int tempid, int gjrid, int mbrid, String mbyhmc, java.util.Date cjsj, boolean sfkfj, String json, int xs, boolean sfjs, int jb, int s, long kszdsj) {
		super();
		this.id = id;
		this.tempid = tempid;
		this.gjrid = gjrid;
		this.mbrid = mbrid;
		this.mbyhmc = mbyhmc;
		this.cjsj = cjsj;
		this.sfkfj = sfkfj;
		this.json = json;
		this.xs = xs;
		this.sfjs = sfjs;
		this.jb = jb;
		this.s = s;
		this.kszdsj = kszdsj;
	}
	
	public Map<String, Object> getUpdateColumns() {
		if(updateColumns == null)
			updateColumns = new HashMap<String, Object>();
		return updateColumns;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
		addUpdateColumn("id",id);
	}
	
	public void changeIdWith(int id){
		this.id += id;
		setId(this.id);
	}
	
	public void changeIdWithMin(int id,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMax(int id,int max){
		this.id += id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMaxMin(int id,int max,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}	
	public int getTempid() {
		return tempid;
	}
	
	public void setTempid(int tempid) {
		this.tempid = tempid;
		addUpdateColumn("tempid",tempid);
	}
	
	public void changeTempidWith(int tempid){
		this.tempid += tempid;
		setTempid(this.tempid);
	}
	
	public void changeTempidWithMin(int tempid,int min){
		this.tempid += tempid;
		this.tempid = this.tempid < min ? min : this.tempid;
		setTempid(this.tempid);
	}
	
	public void changeTempidWithMax(int tempid,int max){
		this.tempid += tempid;
		this.tempid = this.tempid > max ? max : this.tempid;
		setTempid(this.tempid);
	}
	
	public void changeTempidWithMaxMin(int tempid,int max,int min){
		this.tempid += tempid;
		this.tempid = this.tempid < min ? min : this.tempid;
		this.tempid = this.tempid > max ? max : this.tempid;
		setTempid(this.tempid);
	}	
	public int getGjrid() {
		return gjrid;
	}
	
	public void setGjrid(int gjrid) {
		this.gjrid = gjrid;
		addUpdateColumn("攻击人id",gjrid);
	}
	
	public void changeGjridWith(int gjrid){
		this.gjrid += gjrid;
		setGjrid(this.gjrid);
	}
	
	public void changeGjridWithMin(int gjrid,int min){
		this.gjrid += gjrid;
		this.gjrid = this.gjrid < min ? min : this.gjrid;
		setGjrid(this.gjrid);
	}
	
	public void changeGjridWithMax(int gjrid,int max){
		this.gjrid += gjrid;
		this.gjrid = this.gjrid > max ? max : this.gjrid;
		setGjrid(this.gjrid);
	}
	
	public void changeGjridWithMaxMin(int gjrid,int max,int min){
		this.gjrid += gjrid;
		this.gjrid = this.gjrid < min ? min : this.gjrid;
		this.gjrid = this.gjrid > max ? max : this.gjrid;
		setGjrid(this.gjrid);
	}	
	public int getMbrid() {
		return mbrid;
	}
	
	public void setMbrid(int mbrid) {
		this.mbrid = mbrid;
		addUpdateColumn("目标人id",mbrid);
	}
	
	public void changeMbridWith(int mbrid){
		this.mbrid += mbrid;
		setMbrid(this.mbrid);
	}
	
	public void changeMbridWithMin(int mbrid,int min){
		this.mbrid += mbrid;
		this.mbrid = this.mbrid < min ? min : this.mbrid;
		setMbrid(this.mbrid);
	}
	
	public void changeMbridWithMax(int mbrid,int max){
		this.mbrid += mbrid;
		this.mbrid = this.mbrid > max ? max : this.mbrid;
		setMbrid(this.mbrid);
	}
	
	public void changeMbridWithMaxMin(int mbrid,int max,int min){
		this.mbrid += mbrid;
		this.mbrid = this.mbrid < min ? min : this.mbrid;
		this.mbrid = this.mbrid > max ? max : this.mbrid;
		setMbrid(this.mbrid);
	}	
	public String getMbyhmc() {
		return mbyhmc;
	}
	
	public void setMbyhmc(String mbyhmc) {
		this.mbyhmc = mbyhmc;
		addUpdateColumn("目标用户名称",mbyhmc);
	}
	
	public java.util.Date getCjsj() {
		return cjsj;
	}
	
	public void setCjsj(java.util.Date cjsj) {
		this.cjsj = cjsj;
		addUpdateColumn("创建时间",cjsj);
	}
	
	public boolean getSfkfj() {
		return sfkfj;
	}
	
	public void setSfkfj(boolean sfkfj) {
		this.sfkfj = sfkfj;
		addUpdateColumn("是否可反击",sfkfj);
	}
	
	public String getJson() {
		return json;
	}
	
	public void setJson(String json) {
		this.json = json;
		addUpdateColumn("json",json);
	}
	
	public int getXs() {
		return xs;
	}
	
	public void setXs(int xs) {
		this.xs = xs;
		addUpdateColumn("星数",xs);
	}
	
	public void changeXsWith(int xs){
		this.xs += xs;
		setXs(this.xs);
	}
	
	public void changeXsWithMin(int xs,int min){
		this.xs += xs;
		this.xs = this.xs < min ? min : this.xs;
		setXs(this.xs);
	}
	
	public void changeXsWithMax(int xs,int max){
		this.xs += xs;
		this.xs = this.xs > max ? max : this.xs;
		setXs(this.xs);
	}
	
	public void changeXsWithMaxMin(int xs,int max,int min){
		this.xs += xs;
		this.xs = this.xs < min ? min : this.xs;
		this.xs = this.xs > max ? max : this.xs;
		setXs(this.xs);
	}	
	public boolean getSfjs() {
		return sfjs;
	}
	
	public void setSfjs(boolean sfjs) {
		this.sfjs = sfjs;
		addUpdateColumn("是否结算",sfjs);
	}
	
	public int getJb() {
		return jb;
	}
	
	public void setJb(int jb) {
		this.jb = jb;
		addUpdateColumn("金币",jb);
	}
	
	public void changeJbWith(int jb){
		this.jb += jb;
		setJb(this.jb);
	}
	
	public void changeJbWithMin(int jb,int min){
		this.jb += jb;
		this.jb = this.jb < min ? min : this.jb;
		setJb(this.jb);
	}
	
	public void changeJbWithMax(int jb,int max){
		this.jb += jb;
		this.jb = this.jb > max ? max : this.jb;
		setJb(this.jb);
	}
	
	public void changeJbWithMaxMin(int jb,int max,int min){
		this.jb += jb;
		this.jb = this.jb < min ? min : this.jb;
		this.jb = this.jb > max ? max : this.jb;
		setJb(this.jb);
	}	
	public int getS() {
		return s;
	}
	
	public void setS(int s) {
		this.s = s;
		addUpdateColumn("水",s);
	}
	
	public void changeSWith(int s){
		this.s += s;
		setS(this.s);
	}
	
	public void changeSWithMin(int s,int min){
		this.s += s;
		this.s = this.s < min ? min : this.s;
		setS(this.s);
	}
	
	public void changeSWithMax(int s,int max){
		this.s += s;
		this.s = this.s > max ? max : this.s;
		setS(this.s);
	}
	
	public void changeSWithMaxMin(int s,int max,int min){
		this.s += s;
		this.s = this.s < min ? min : this.s;
		this.s = this.s > max ? max : this.s;
		setS(this.s);
	}	
	public long getKszdsj() {
		return kszdsj;
	}
	
	public void setKszdsj(long kszdsj) {
		this.kszdsj = kszdsj;
		addUpdateColumn("开始战斗时间",kszdsj);
	}
	
	public void changeKszdsjWith(long kszdsj){
		this.kszdsj += kszdsj;
		setKszdsj(this.kszdsj);
	}
	
	public void changeKszdsjWithMin(long kszdsj,long min){
		this.kszdsj += kszdsj;
		this.kszdsj = this.kszdsj < min ? min : this.kszdsj;
		setKszdsj(this.kszdsj);
	}
	
	public void changeKszdsjWithMax(long kszdsj,long max){
		this.kszdsj += kszdsj;
		this.kszdsj = this.kszdsj > max ? max : this.kszdsj;
		setKszdsj(this.kszdsj);
	}
	
	public void changeKszdsjWithMaxMin(long kszdsj,long max,long min){
		this.kszdsj += kszdsj;
		this.kszdsj = this.kszdsj < min ? min : this.kszdsj;
		this.kszdsj = this.kszdsj > max ? max : this.kszdsj;
		setKszdsj(this.kszdsj);
	}	
	
	//攻击人id
	public Yh getYhPkGjrid(){
		return YhCache.getById(gjrid);
	}
	//目标人id
	public Yh getYhPkMbrid(){
		return YhCache.getById(mbrid);
	}
	public void addUpdateColumn(String key, Object val) {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.put(key, val);
	}
	
	public void clearUpdateColumn() {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.clear();
	}
	
	public Map<String, Object> toMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("tempid", this.tempid);
        result.put("gjrid", this.gjrid);
        result.put("mbrid", this.mbrid);
        result.put("mbyhmc", this.mbyhmc);
        result.put("cjsj", this.cjsj);
        result.put("sfkfj", this.sfkfj);
        result.put("json", this.json);
        result.put("xs", this.xs);
        result.put("sfjs", this.sfjs);
        result.put("jb", this.jb);
        result.put("s", this.s);
        result.put("kszdsj", this.kszdsj);
        return result;
    }
    
    public String toString(){
        return toMap().toString();
    }
    
    public String toJson(){
    	return JSON.toJSONString(toMap());
    }
    
    /**
     * 数据库源字段Map
     */
    public Map<String, Object> toColumnNameMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("tempid", this.tempid);
        result.put("攻击人id", this.gjrid);
        result.put("目标人id", this.mbrid);
        result.put("目标用户名称", this.mbyhmc);
        result.put("创建时间", this.cjsj);
        result.put("是否可反击", this.sfkfj);
        result.put("json", this.json);
        result.put("星数", this.xs);
        result.put("是否结算", this.sfjs);
        result.put("金币", this.jb);
        result.put("水", this.s);
        result.put("开始战斗时间", this.kszdsj);
        return result;
    }
    
    public String toColumnNameString(){
        return toColumnNameMap().toString();
    }
    
    public byte[] toBytes() throws Exception {
    	try (ByteArrayOutputStream out = new ByteArrayOutputStream();) {
		    SK_OutputStream.writeInt(out,this.id);
		    SK_OutputStream.writeInt(out,this.tempid);
		    SK_OutputStream.writeInt(out,this.gjrid);
		    SK_OutputStream.writeInt(out,this.mbrid);
		    SK_OutputStream.writeString(out,this.mbyhmc);
		    SK_OutputStream.writeString(out,this.json);
		    SK_OutputStream.writeInt(out,this.xs);
		    SK_OutputStream.writeInt(out,this.jb);
		    SK_OutputStream.writeInt(out,this.s);
		    SK_OutputStream.writeLong(out,this.kszdsj);
		    return out.toByteArray();
    	}catch (Exception e) {
            throw e;
        }
    }
    
     public static Zb createForBytes(byte[] _byte) throws Exception {
     	try (ByteArrayInputStream in = new ByteArrayInputStream(_byte);) {
	     	Zb zb = new Zb();
		    zb.id = SK_InputStream.readInt(in,null);
		    zb.tempid = SK_InputStream.readInt(in,null);
		    zb.gjrid = SK_InputStream.readInt(in,null);
		    zb.mbrid = SK_InputStream.readInt(in,null);
		    zb.mbyhmc = SK_InputStream.readString(in,null);
		    zb.json = SK_InputStream.readString(in,null);
		    zb.xs = SK_InputStream.readInt(in,null);
		    zb.jb = SK_InputStream.readInt(in,null);
		    zb.s = SK_InputStream.readInt(in,null);
		    zb.kszdsj = SK_InputStream.readLong(in,null);
		    return zb;
    	}catch (Exception e) {
            throw e;
        }
     }
    
    /**
     * 根据list创建对象
     */
    public static List<Zb> createForColumnNameList(List<Map<String, Object>> list){
    	List<Zb> zbs = new ArrayList<Zb>();
		for (Map<String, Object> map : list) {
			zbs.add(createForColumnNameMap(map));
		}
		return zbs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Zb createForColumnNameMap(Map<String, Object> map){
    	Zb obj = new Zb();
	    obj.id = SK_Map.getInt("id", map);
	    obj.tempid = SK_Map.getInt("tempid", map);
	    obj.gjrid = SK_Map.getInt("攻击人id", map);
	    obj.mbrid = SK_Map.getInt("目标人id", map);
	    obj.mbyhmc = SK_Map.getString("目标用户名称", map);
	    obj.json = SK_Map.getString("json", map);
	    obj.xs = SK_Map.getInt("星数", map);
	    obj.jb = SK_Map.getInt("金币", map);
	    obj.s = SK_Map.getInt("水", map);
	    obj.kszdsj = SK_Map.getLong("开始战斗时间", map);
        return obj;
    }
    
    /**
     * 根据list创建对象
     */
    public static List<Zb> createForList(List<Map<String, Object>> list){
    	List<Zb> zbs = new ArrayList<Zb>();
		for (Map<String, Object> map : list) {
			zbs.add(createForColumnNameMap(map));
		}
		return zbs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Zb createForMap(Map<String, Object> map){
    	Zb obj = new Zb();
	    obj.id = SK_Map.getInt("id", map);
	    obj.tempid = SK_Map.getInt("tempid", map);
	    obj.gjrid = SK_Map.getInt("gjrid", map);
	    obj.mbrid = SK_Map.getInt("mbrid", map);
	    obj.mbyhmc = SK_Map.getString("mbyhmc", map);
	    obj.json = SK_Map.getString("json", map);
	    obj.xs = SK_Map.getInt("xs", map);
	    obj.jb = SK_Map.getInt("jb", map);
	    obj.s = SK_Map.getInt("s", map);
	    obj.kszdsj = SK_Map.getLong("kszdsj", map);
        return obj;
    }
    
    public static Zb createForJson(String json){
    	Map<String, Object> map = JSON.parseObject(json);
    	return createForMap(map);
    }
    
    public static List<Zb> createForJson(List<String> jsons){
    	List<Zb> zbs = new ArrayList<Zb>();
    	for(String json : jsons){
    		zbs.add(createForJson(json));
    	}
    	return zbs;
    }
    
    /** 延迟插入数据库 */
    public Zb insert(){
    	return ZbCache.insert(this);
    }
    /** 延迟更新数据库 */
    public Zb update(){
    	return ZbCache.update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return ZbCache.delete(this);
    }
    /** 即时插入数据库 */
    public Zb insertAndFlush(){
    	return ZbCache.insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public Zb updateAndFlush(){
    	return ZbCache.updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return ZbCache.deleteAndFlush(this);
    }
}